package com.chenyaowei.payment.config;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * 使用@PropertySource注解注入的资源文件默认是使用DefaultPropertySourceFactory进行加载解读的，
 * 这个类中默认是建立ResourcePropertySource去解析资源文件，但是这个类是用来解析.property文件，用这个类来解析.yml文件不能得到正确的内容。
 * 需要自己定义一个解析yml文件的解析类，并在@PropertySource注解中进行引用。CustomYmlParseFactory.class
 **/
@Component
@ConfigurationProperties(prefix = "alipay")
@PropertySource(value = "classpath:alipay.yml", encoding = "utf-8", factory = CustomYmlParseFactory.class)
public class AlipayConfig {

    /** 商户 appId */
    private String appId;
    /** 私钥 RSA2格式的 */
    private String privateKey;
    /** 支付宝公钥 */
    private String publicKey;
    /** 返回格式 json */
    private String format;
    /** RSA2 */
    private String signType;
    /** 编码 */
    private String charset;
    /** 请求网关地址 */
    private String gatewayUrl;
    /** 日志记录目录定义在 logFile 中 */
    private static String logPath;
    /** 同步跳转地址，仅支持http/https */
    private static String returnUrl;
    /** 异步接收地址，仅支持http/https，公网可访问 */
    private static String notifyUrl;

    @Bean
    public AlipayClient alipayClient(){
        return new DefaultAlipayClient(gatewayUrl, appId, privateKey, format, charset, publicKey, signType);
    }

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getPrivateKey() {
        return privateKey;
    }

    public void setPrivateKey(String privateKey) {
        this.privateKey = privateKey;
    }

    public String getPublicKey() {
        return publicKey;
    }

    public void setPublicKey(String publicKey) {
        this.publicKey = publicKey;
    }

    public String getFormat() {
        return format;
    }

    public void setFormat(String format) {
        this.format = format;
    }

    public String getSignType() {
        return signType;
    }

    public void setSignType(String signType) {
        this.signType = signType;
    }

    public String getCharset() {
        return charset;
    }

    public void setCharset(String charset) {
        this.charset = charset;
    }

    public String getGatewayUrl() {
        return gatewayUrl;
    }

    public void setGatewayUrl(String gatewayUrl) {
        this.gatewayUrl = gatewayUrl;
    }

    public static String getLogPath() {
        return logPath;
    }

    public void setLogPath(String logPath) {
        AlipayConfig.logPath = logPath;
    }

    public static String getReturnUrl() {
        return returnUrl;
    }

    public void setReturnUrl(String returnUrl) {
        AlipayConfig.returnUrl = returnUrl;
    }

    public static String getNotifyUrl() {
        return notifyUrl;
    }

    public void setNotifyUrl(String notifyUrl) {
        AlipayConfig.notifyUrl = notifyUrl;
    }

}
